package upcweb.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import upcweb.modelo.ClsResponsableFamilia;
import upcweb.negocio.BsResponsableFamilia;

@Controller
public class ResponsableController {

	BsResponsableFamilia bsRespFamilia = new BsResponsableFamilia();
	
	@RequestMapping(value = "/listarResponsable" , method = RequestMethod.POST)
	protected ModelAndView listarResponsable(){
		ModelAndView view = new ModelAndView("ResponsableFamilia/mvListadoResponsable");
				
		ArrayList<ClsResponsableFamilia> olstRespFam = new ArrayList<ClsResponsableFamilia>();
		
		ClsResponsableFamilia entRespFam = new ClsResponsableFamilia();
		
		entRespFam.setCo_responsable(0);
		entRespFam.setCo_tipo_documento(0);
		entRespFam.setNu_documento("");
		entRespFam.setNo_responsable("");
		entRespFam.setApe_paterno("");
		entRespFam.setApe_materno("");
		entRespFam.setNo_correo("");		
		entRespFam.setFl_activo("");
							
		olstRespFam = bsRespFamilia.BuscarResponsableFamilia(entRespFam);		
		view.addObject("regresar", "c");
		view.addObject("ListadoResponsable", olstRespFam);
		view.addObject("ParamBusqueda", entRespFam);
		
		return view;
	}
	
	@RequestMapping(value = "/BuscarResponsable" , method = RequestMethod.POST)
	protected ModelAndView BuscarResponsable(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView view = null;
		if (request.getParameter("opcion").equals("b")){
			view = new ModelAndView("ResponsableFamilia/mvBusquedaResponsable");
			view.addObject("busqueda", "b");
		}
		else if (request.getParameter("opcion").equals("e")){
			view = new ModelAndView("ResponsableFamilia/mvMantenimientoResponsable");
			view.addObject("editar", "e");
		}
		else if (request.getParameter("opcion").equals("c")){
			view = new ModelAndView("ResponsableFamilia/mvListadoResponsable");
			view.addObject("regresar", "c");
		}
		
		ArrayList<ClsResponsableFamilia> olstRespFam = new ArrayList<ClsResponsableFamilia>();
		
		ClsResponsableFamilia entRespFam = new ClsResponsableFamilia();
		
		entRespFam.setCo_responsable(Integer.parseInt(request.getParameter("co_responsable")));
		entRespFam.setCo_tipo_documento(Integer.parseInt(request.getParameter("cbo_TipoDocumento")));
		entRespFam.setNu_documento(request.getParameter("txt_NumeroDocumento").trim());
		entRespFam.setNo_responsable(request.getParameter("txt_Nombres").trim());
		entRespFam.setApe_paterno(request.getParameter("txt_ApePaterno").trim());
		entRespFam.setApe_materno(request.getParameter("txt_ApeMaterno").trim());
		entRespFam.setNo_correo(request.getParameter("txt_correo").trim());
		entRespFam.setFl_activo(request.getParameter("cbo_Estado"));
									
		olstRespFam = bsRespFamilia.BuscarResponsableFamilia(entRespFam);
		view.addObject("ListadoResponsable", olstRespFam);
		view.addObject("ParamBusqueda", entRespFam);
		return view;
	}
	
	@RequestMapping(value = "/mantenimientoResponsable", method = RequestMethod.POST)
	protected ModelAndView mantenimientoResponsable(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("ResponsableFamilia/mvMantenimientoResponsable");	
		return view;
	}
	
	@RequestMapping(value = "/OperacionResponsable", method = RequestMethod.POST)
	protected ModelAndView MantResponsable(HttpServletRequest request,
			HttpServletResponse response){
				
		ClsResponsableFamilia objEntResp = new ClsResponsableFamilia();
		String opcion =  String.valueOf(request.getParameter("opcion"));
		objEntResp.setCo_responsable(Integer.parseInt(request.getParameter("co_responsable")));
		objEntResp.setCo_tipo_documento(Integer.parseInt(request.getParameter("cbo_TipoDocumento")));
		objEntResp.setNu_documento(request.getParameter("txt_NumeroDocumento").trim());
		objEntResp.setNo_responsable(request.getParameter("txt_Nombres").trim());		
		objEntResp.setApe_paterno(request.getParameter("txt_ApePaterno").trim());
		objEntResp.setApe_materno(request.getParameter("txt_ApeMaterno").trim());
		objEntResp.setNo_correo(request.getParameter("txt_correo").trim());
		objEntResp.setNo_password(request.getParameter("txt_Password").trim());
		objEntResp.setCo_sexo(request.getParameter("cbo_Sexo").trim());		
		objEntResp.setNu_celular(request.getParameter("txt_Celular").trim());
		objEntResp.setTx_direccion(request.getParameter("txt_Direccion").trim());
		objEntResp.setFl_activo(request.getParameter("cbo_Estado").trim());
		objEntResp.setNo_usuario(request.getParameter("txt_Usuario").trim());
		
		String resultado = bsRespFamilia.MantResponsableFamilia(opcion, objEntResp);
		System.out.println("resultado: " + resultado);
				
		ModelAndView view = new ModelAndView("ResponsableFamilia/opMantenimientoResponsable");
			
		view.addObject("resultado",resultado);
		view.addObject("opcion",opcion);
		return view;
	}
	
}
